// pages/home/home.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchField: "", 
        msg1: 'tuijain',
        lostlist: null,
        lostShow: null,

    },
    onChange(event){
        // console.log(event);
        this.setData({searchField: event.detail.value});
    },
    tuijianOnchange(event){
      // console.log(event);
      var nextType = event.detail.name;
      if(nextType === "tuijian"){// 推荐
        console.log("推荐");
        this.changeTuiJian();
      }
      if(nextType === "age"){//年龄
        console.log("年龄");
        this.changeAge(0, 20);
      }
      if(nextType === "gender"){//姓别
        console.log("姓别");
        this.changeGender("男");
      }
      
      if(nextType === "area"){//地区
        console.log("地区");
        this.changeTuiJian();
      }
    },
    genderOnchange(event){
      console.log(event);
      var nextType = event.detail.name;
      if(nextType === "boy"){// 男孩
        console.log("男");
        this.changeGender("男");
      }
      if(nextType === "girl"){//女孩
        console.log("女");
        this.changeGender("女");
      }
    },

    ageOnchange(event){
      console.log(event);
      var nextType = event.detail.name;
      if(nextType === 0){// 0-20
        console.log("0-20");
        this.changeAge(0, 20);
      }
      if(nextType === 1){//20-40
        console.log("20-40");
        this.changeAge(20, 40);

      }
      if(nextType === 2){// 40-60
        console.log("40-60");
        this.changeAge(40, 60);

      }
      if(nextType === 3){//60+
        console.log("60+");
        this.changeAge(60, 200);

      }
    },
    changeGender(gender){
      var losts = new Array();
      if(this.data.lostlist === null) return;
      for(let i = 0; i < this.data.lostlist.length; i++){
        const obj = this.data.lostlist[i];
        // console.log(obj);
        if(obj.gender === gender)
          losts.push(obj);
      }
      losts = this.modifyIndex(losts);
      console.log(losts);
      // console.log(this.data.lostlist);
      this.setData({
        lostShow: losts
      })
    },
    gotoDetailPublish(event){
      var lostId = event.currentTarget.dataset.id;
      console.log(lostId);
      wx.navigateTo({
        url: '/pages/detailPublish/detailPublish?lostId=' + lostId + '&whereFrom=myPublish',
      })
    },
    changeAge(ageMin, ageMax){
      var losts = new Array();
      if(this.data.lostlist === null) return;
      var nowDate = new Date();
      var nowYear = nowDate.getFullYear();
      // console.log(nowYear);
      for(let i = 0; i < this.data.lostlist.length; i++){
        const obj = this.data.lostlist[i];
        // console.log(obj);
        let peopleYear = parseInt(obj.birthday.substring(0, 4))
        // console.log(peopleYear);
        if(peopleYear + ageMin <= nowYear && peopleYear + ageMax >= nowYear)
          losts.push(obj);
      }
      losts = this.modifyIndex(losts);
      console.log(losts);
      // console.log(this.data.lostlist);
      this.setData({
        lostShow: losts
      })
    },
    changeTuiJian(){
      var losts = this.data.lostlist;
      losts = this.modifyIndex(losts);
      this.setData({
        lostShow: losts
      })
    },


    modifyIndex(losts){
      for(let i = 0; i < losts.length; i++){
        losts[i].index = i +1;
      }
      return losts;
    },
    onSearch(){
        // 校验是否输入了内容
        if (this.data.searchField === "" || this.data.searchField === null){
            wx.showToast({
              title: '查询内容为空',
              icon: "error",
              duration: 1000
            })
        }
        else{
            wx.navigateTo({
                url: '/pages/search/search?searchField=' + this.data.searchField,
            })
        }
        
    },
    getLost(){
      wx.request({
        url: app.globalData.URL_PRE + '/publish',
        method: 'GET',
        header:{
            'cookie': wx.getStorageSync('cookie')
        },
        success:(res)=>{
            if(res.data.code === 200){
              //   console.log(res.data.data);
                this.setData({
                  lostlist: res.data.data,
                  lostShow: res.data.data
                })

            }
        }
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.getLost();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.setNavigationBarTitle({
          title: '首页',
        })
        this.getLost();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})